package com.david.service;

import com.david.dao.MessageDao;
import com.david.entity.Category;
import com.david.entity.Message;
import com.david.model.api.PageOf;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MessageService {

    @Resource
    MessageDao messageDao;

    public PageOf<Message> findByType(int type, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        int count = messageDao.findByTypeCount(type);

        PageOf<Message> result = new PageOf<Message>();

        PageHelper.startPage(page, pageSize);
        List<Message> dataList = messageDao.findByType(type);

        result.Data = dataList;
        result.Page = page;
        result.RowsCount = pageSize;
        result.TotalCount = count;
        result.TotalPages = (int) Math.ceil(result.TotalCount / pageSize);
        return result;
    }


    public Message findById(Integer id) {
        return messageDao.findById(id);
    }

    public int insert(Message message) {
        return messageDao.insert(message);
    }

}
